//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Imaging;
using DNAide.Core;

namespace DNAide.Drawing
{

	public static partial class BitmapAide
	{

		/// <summary>
		/// Resizes and crops to fit required dimensions. Useful for resizing portrait to landscape and vice versa
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		public static void ResizeAndCropToFit(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight)
		{
			ResizeAndCropToFit(ref bitmapToResize, newWidth, newHeight, ResizeAndCropToFitPosition.Center);
		}


		/// <summary>
		/// Resizes and crops to fit required dimensions. Useful for resizing portrait to landscape and vice versa
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		/// <param name="cropPosition"></param>
		public static void ResizeAndCropToFit(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight, ResizeAndCropToFitPosition cropPosition)
		{
			ResizeAndCropToFit(ref bitmapToResize, newWidth, newHeight, cropPosition, new GraphicsSettings());
		}


		/// <summary>
		/// Resizes and crops to fit required dimensions. Useful for resizing portrait to landscape and vice versa
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		/// <param name="cropPosition"></param>
		/// <param name="graphicsSettings"></param>
		public static void ResizeAndCropToFit(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight, ResizeAndCropToFitPosition cropPosition, GraphicsSettings graphicsSettings)
		{
			int calcWidth = 0;
			int calcHeight = 0;

			bool cropVertically = true;

			// Resize the bitmap so that the smallest dimension is scaled to the largest dimension
			if (((float)bitmapToResize.Width / (float)newWidth) < ((float)bitmapToResize.Height / (float)newHeight))
			{
				calcWidth = newWidth;
				calcHeight = (int)((float)bitmapToResize.Height * ((float)newWidth / (float)bitmapToResize.Width));
			}
			else
			{
				calcHeight = newHeight;
				calcWidth = (int)((float)bitmapToResize.Width * ((float)newHeight / (float)bitmapToResize.Height));
				cropVertically = false;
			}

			BitmapAide.Resize(ref bitmapToResize, calcWidth, calcHeight, false, graphicsSettings);

			int cropX = 0; // Left
			int cropY = 0; // Top

			if (cropVertically)
			{
				if (cropPosition == ResizeAndCropToFitPosition.BottomOrRight)
				{
					cropY = calcHeight - newHeight; // Bottom
				}
				else if (cropPosition == ResizeAndCropToFitPosition.Center)
				{
					cropY = (int)((float)(calcHeight - newHeight) / 2); // Center
				}
			}
			else
			{
				if (cropPosition == ResizeAndCropToFitPosition.BottomOrRight)
				{
					cropX = calcWidth - newWidth; // Right
				}
				else if (cropPosition == ResizeAndCropToFitPosition.Center)
				{
					cropX = (int)((float)(calcWidth - newWidth) / 2); // Center
				}
			}

			BitmapAide.Crop(ref bitmapToResize, cropX, cropY, newWidth, newHeight);

		}


		/// <summary>
		/// Resizes and crops to fit required dimensions. Useful for resizing portrait to landscape and vice versa
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		/// <param name="cropOffset"></param>
		public static void ResizeAndCropToFit(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight, int cropOffset)
		{
			ResizeAndCropToFit(ref bitmapToResize, newWidth, newHeight, cropOffset);
		}


		/// <summary>
		/// Resizes and crops to fit required dimensions. Useful for resizing portrait to landscape and vice versa
		/// </summary>
		/// <param name="bitmapToResize"></param>
		/// <param name="newWidth"></param>
		/// <param name="newHeight"></param>
		/// <param name="cropOffset"></param>
		/// <param name="graphicsSettings"></param>
		public static void ResizeAndCropToFit(ref Bitmap bitmapToResize, Int32 newWidth, Int32 newHeight, int cropOffset, GraphicsSettings graphicsSettings)
		{
			int calcWidth = 0;
			int calcHeight = 0;

			bool cropVertically = true;

			// Resize the bitmap so that the smallest dimension is scaled to the largest dimension
			if (((float)bitmapToResize.Width / (float)newWidth) < ((float)bitmapToResize.Height / (float)newHeight))
			{
				calcWidth = newWidth;
				calcHeight = (int)((float)bitmapToResize.Height * ((float)newWidth / (float)bitmapToResize.Width));
			}
			else
			{
				calcHeight = newHeight;
				calcWidth = (int)((float)bitmapToResize.Width * ((float)newHeight / (float)bitmapToResize.Height));
				cropVertically = false;
			}

			BitmapAide.Resize(ref bitmapToResize, calcWidth, calcHeight, false, graphicsSettings);

			int cropX = 0; // Left
			int cropY = 0; // Top

			if (cropVertically)
			{
				cropY = cropOffset;
			}
			else
			{
				cropX = cropOffset;
			}

			BitmapAide.Crop(ref bitmapToResize, cropX, cropY, newWidth, newHeight);

		}

	}

}
